home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Utilities / Winter Shell 1.0d2 / Source / Libraries / StringLib / StringLib.c next >
Encoding:
C/C++ Source or Header  |  1993-11-30  |  1.2 KB  |  48 lines  |  [TEXT/KAHL]

  1. /* Functions for doing things to strings.
  2.     
  3.     Revision History:
  4.     
  5.     91/07/02 AIH
  6.     - Cast (-1) to ((size_t) -1) just to make sure it wasn't being interpreted
  7.     as an integer
  8.     
  9.     91/03/01 AIH
  10.     - Added require/ensure statements to StrFit
  11.     
  12.     91/01/21 AIH
  13.     - Added brief comment describing this file
  14.     
  15.     91/01/05 Ari Halberstadt (AIH)
  16.     - Inserted this standard header in all files */
  17.  
  18. #include <stddef.h>
  19. #include <string.h>
  20. #include "MemoryLib.h"
  21. #include "StringLib.h"
  22.  
  23. /* true if string is valid and is not longer than length (-1 for any length) */
  24. Boolean StrValid(const char *str, size_t length)
  25. {
  26.     return(MemValid(str) && (length == (size_t) -1 || strlen(str) < length));
  27. }
  28.  
  29. /* fit string to width by truncating it and adding the extra character */
  30. void StrFit(CStr255 str, short maxwidth, char extra)
  31. {
  32.     short width;    /* width of string */
  33.     short len;        /* length of string */
  34.     Str255 pstr;
  35.     
  36.     require(StrValid(str, sizeof(CStr255)));
  37.     c2pstr(strcpy((char*)pstr, str));
  38.     width = StringWidth(pstr);
  39.     if (width > maxwidth) {
  40.         width += CharWidth(extra);
  41.         for (len = *pstr; len > 0 && width > maxwidth; len--)
  42.             width -= CharWidth(str[len-1]);
  43.         str[len] = extra;
  44.         str[len+1] = 0;
  45.     }
  46.     ensure(width <= maxwidth);
  47. }
  48.